package com.nav.brainfuckeditor.editors;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;

public class BFTextHover implements ITextHover {
	private static final String BF_OPEN = IBFCharConstants.BF_OPEN + ": if the byte at the data pointer is zero, then jump the instruction pointer forward to the matching " + IBFCharConstants.BF_CLOSE + " command.";
	private static final String BF_CLOSE = IBFCharConstants.BF_CLOSE + ": if the byte at the data pointer is nonzero, then jump the instruction pointer back to the matching " + IBFCharConstants.BF_OPEN + " command.";
	private static final String BF_INPUT = IBFCharConstants.BF_INPUT + ": accept one byte of input, storing its value in the byte at the data pointer.";
	private static final String BF_OUTPUT = IBFCharConstants.BF_OUTPUT + ": output the byte at the data pointer.";
	private static final String BF_LEFT = IBFCharConstants.BF_LEFT + ": decrement the data pointer (to point to the next cell to the left).";
	private static final String BF_RIGHT = IBFCharConstants.BF_RIGHT + ": increment the data pointer (to point to the next cell to the right).";
	private static final String BF_INCREASE = IBFCharConstants.BF_INCREASE + ": increment (increase by one) the byte at the data pointer.";
	private static final String BF_DECREASE = IBFCharConstants.BF_DECREASE + ": decrement (decrease by one) the byte at the data pointer.";
	@Override
	public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
		try {
			char c = textViewer.getDocument().getChar(hoverRegion.getOffset());
			switch(c) {
			case IBFCharConstants.BF_OPEN:
				return BF_OPEN;
			case IBFCharConstants.BF_CLOSE:
				return BF_CLOSE;
			case IBFCharConstants.BF_INPUT:
				return BF_INPUT;
			case IBFCharConstants.BF_OUTPUT:
				return BF_OUTPUT;
			case IBFCharConstants.BF_LEFT:
				return BF_LEFT;
			case IBFCharConstants.BF_RIGHT:
				return BF_RIGHT;
			case IBFCharConstants.BF_INCREASE:
				return BF_INCREASE;
			case IBFCharConstants.BF_DECREASE:
				return BF_DECREASE;
			default:
					return null;
			}
		} catch(BadLocationException e) {
			// not expecting as the pos value is always correct, therefore ignoring
		}
		return null;
	}

	@Override
	public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
		return new Region(offset, 1);
	}
}
